import tracker from "../utils/tracker";
import getSelector from "../utils/getSelector";
import {onload} from "../utils/onload";

export function blankScreen(){
  // 横向取9个元素 纵向取9个元素
  // 查看 elementsFromPoint.png

  onload(()=>{
    emptyPoints = 0;

    for (let i = 0; i < 9; i++) {
      const xElements = document.elementsFromPoint(
        window.innerWidth * i / 10, //横坐标
        window.innerHeight / 2 //纵坐标
      );
      const yElements = document.elementsFromPoint(
        window.innerWidth / 2,
        window.innerHeight * i / 10
      );
      isWrapper(xElements[0]); //elementsFromPoint方法可以获取到当前视口内指定坐标处，由里到外排列的所有元素 ---> SO取到的是一个集合, 故取[0]
      isWrapper(yElements[0]);
    }

    if (emptyPoints > 16/*阈值可以自己定义,并不一定要等于18,少一点也可算白屏*/) {
      const centerElements = document.elementsFromPoint(window.innerWidth / 2, window.innerHeight / 2);
      tracker.send({
        kind: 'stability',
        type: 'blank',
        emptyPoints,
        screen: window.screen.width + 'X' + window.screen.height,
        viewPoint: window.innerWidth + 'X' + window.innerHeight,
        selector: getSelector(centerElements[0])
      })
    }
  })
}

let wrapperElements = ['html', 'body', '#container', '.content.main']; //todo 柯里化出去 让用户传参
let emptyPoints = 0;
function isWrapper(element){
  const selector = getSelector(element);
  if (wrapperElements.indexOf(selector) !== -1) { //该点由到外的第一个元素 是 wrapperElements 中的一种, 也就是说该点没有实际内容, 也就是空白点
    emptyPoints++;
  }
}

